<?php
// Register widgetized areas
if ( function_exists('register_sidebar') ) {
	$sidebar_widget_arr = array();
	
///-----------------------------------------------------------------
	  //  HEADER SECTION WIDGET AREAS  //
///----------------------------------------------------------------

	$sidebar_widget_arr['header_logo_center_side'] =array(1,array('name' => __('Header: Center area','templatic'),'description' => 'The centermost section alongside the logo. A Multi city box or a login form can go here.','id' => 'header_logo_center_side','before_widget' => '<div class="widget">','after_widget' => '</div>','before_title' => '<h3>','after_title' => '</h3>'));

	$sidebar_widget_arr = apply_filters('templ_sidebar_widget_box_filter',$sidebar_widget_arr); //Sidebar widget area manage filer
	foreach($sidebar_widget_arr as $key=>$val)
	{
		if($val){
		register_sidebars($val[0],$val[1]);
		}
	}
}

class templ_latest_posts_grid_custom_view extends WP_Widget {
	
		function templ_latest_posts_grid_custom_view() {
		//Constructor
		global $thumb_url;
			$widget_ops = array('classname' => 'widget special', 'description' => apply_filters('templ_latestpost_with_img_widget_desc_filter',__('Show place or event home page featured listings in a grid layout.','templatic')) );
			$this->WP_Widget('latest_posts_grid_custom_view',apply_filters('templ_latestpost_with_img_widget_title_filter',__('T &rarr;Custom Featured Listings For Home Page(Grid view)','templatic')), $widget_ops);
		}
	 
		function widget($args, $instance) {
		// prints the widget
			
			extract($args, EXTR_SKIP);
	 
			//echo $before_widget;
			$title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
			$category = empty($instance['category']) ? '' : apply_filters('widget_category', $instance['category']);
			$number = empty($instance['number']) ? '5' : apply_filters('widget_number', $instance['number']);
			$my_post_type = empty($instance['post_type']) ? 'event' : apply_filters('widget_post_type', $instance['post_type']);
			$link = empty($instance['link']) ? '' : apply_filters('widget_link', $instance['link']);
			$text = empty($instance['text']) ? '' : apply_filters('widget_text', $instance['text']);
			global $post,$wpdb;
			$post_widget_count = 1;

				if($my_post_type == CUSTOM_POST_TYPE1){
				$my_post_cat = 'placecategory';
				}else{
				$my_post_cat = 'eventcategory';
				}
				$args=array($my_post_cat => $category,
				  'post_type' => $my_post_type,
				  'posts_per_page' => $number,
				  'caller_get_posts'=> 1);
				$my_query = null;
				$my_query = new WP_Query($args);
				if( $my_query->have_posts() ) {
				 ?>
					
         <div id="loop" class="grid clear">	
				<?php if($title){?> 
					<h3><span><?php _e($title,'templatic');?></span>
					<?php if($link){?><a href="<?php _e($link,'templatic');?>" class="more" ><?php _e($text,'templatic');?></a><?php }?>
         
					</h3> <?php }?>
			
			<?php   while ($my_query->have_posts()) : $my_query->the_post();
					$post_images =  bdw_get_images_with_info($post->ID,'large');   
					$attachment_id = $post_images[0]['id']; ?>	
					<div id="post_<?php the_ID(); ?>" <?php if((get_post_meta($post->ID,'is_featured',true) == 1) && (get_post_meta($post->ID,'featured_type',true) =="both" || get_post_meta($post->ID,'featured_type',true) =="h")){ post_class('post featured_post');} else { post_class('post');}?>>
					<div class="post-content">
					<?php if((get_post_meta($post->ID,'is_featured',true) ==1) && ( get_post_meta($post->ID,'featured_type',true) =="both" || get_post_meta($post->ID,'featured_type',true) =="h")){?>
							<span class="featured_img"><?php _e('featured','templatic');?></span>
					<?php }
					$alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
					$attach_data = get_post($attachment_id);
					$title = $attach_data->post_title;
					$width = get_option('thumbnail_size_w');
					$height = get_option('thumbnail_size_h');
					$is_crop = get_option('thumbnail_crop');
					if($is_crop == 1){ $is_crop = 'true'; }else{ $is_crop = 'false'; }
					if($title ==''){ $title = $post->post_title; }
					if($alt ==''){ $alt = $post->post_title; }
					if($post_images[0]['file']){  
					$crop_image = vt_resize($attachment_id, $post_images[0]['file'], $width, $height, $crop = $is_crop );
					?>
						<a class="post_img" href="<?php the_permalink(); ?>"><img  src="<?php echo $crop_image['url'];?>" alt="<?php $alt; ?>" title="<?php echo $title; ?>"  /> </a>
					
					<?php
					}else {?>
						<a class="img_no_available" href="<?php the_permalink(); ?>"> <?php _e('Image Not Available','templatic')?> </a>
					<?php } ?>
						
					<div class="post_content">
						<h2><a class="widget-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
					</div>     
					</div>                      
					</div>
          <?php  
		  
		  if($post_widget_count == '3') {
				echo '<div class="hr clearfix"></div>';
				$post_widget_count = 0;
            } 
            
	 $post_widget_count++;
	 endwhile; wp_reset_query(); ?>
	</div>
	<?php }
	
	 //echo $after_widget;
		}
		function update($new_instance, $old_instance) {
		//save the widget
			$instance = $old_instance;
			$instance['title'] = strip_tags($new_instance['title']);
			$instance['category'] = strip_tags($new_instance['category']);
			$instance['number'] = strip_tags($new_instance['number']);
			$instance['post_type'] = strip_tags($new_instance['post_type']);
			$instance['link'] = strip_tags($new_instance['link']);
			$instance['text'] = strip_tags($new_instance['text']);
			return $instance;
		}
	 
		function form($instance) {
		//widgetform in backend
			$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'category' => '', 'number' => '' ) );
			$title = strip_tags($instance['title']);
			$category = strip_tags($instance['category']);
			$number = strip_tags($instance['number']);
			$my_post_type = strip_tags($instance['post_type']);
			$link = strip_tags($instance['link']);
			$text = strip_tags($instance['text']);
	?>
	<p>
		<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:','templatic');?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
		<p><label for="<?php echo $this->get_field_id('text'); ?>"><?php _e('View All Text :','templatic');?> <input class="widefat" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>" type="text" value="<?php echo attribute_escape($text); ?>" /></label></p>
		<p><label for="<?php echo $this->get_field_id('link'); ?>"><?php _e('View All Link URL (ex.http://templatic.com/events):','templatic');?> <input class="widefat" id="<?php echo $this->get_field_id('link'); ?>" name="<?php echo $this->get_field_name('link'); ?>" type="text" value="<?php echo attribute_escape($link); ?>" /></label>
	</p>
	<p>
	  <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts:','templatic');?>
	  <input class="widefat" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo attribute_escape($number); ?>" />
	  </label>
	</p>
    <p>
		<label for="<?php echo $this->get_field_id('post_type'); ?>"><?php _e('Post Type:','templatic')?>
		<select id="<?php echo $this->get_field_id('post_type'); ?>" name="<?php echo $this->get_field_name('post_type'); ?>">
		<?php
		$custom_post_types_args = array();  
		$custom_post_types = get_post_types($custom_post_types_args,'objects');   
		foreach ($custom_post_types as $content_type) {
		if($content_type->name!='nav_menu_item' && $content_type->name!='attachment' && $content_type->name!='revision' && $content_type->name!='page' && $content_type->name!='post'){
		?>
			<option value="<?php _e($content_type->name);?>" <?php if(attribute_escape($my_post_type)==$content_type->name){ echo 'selected="selected"';}?>><?php _e($content_type->label);?></option>
		<?php }}?>
		</select>
		</label>
	</p>
	<p>
	  <label for="<?php echo $this->get_field_id('category'); ?>"><?php _e('Categories (<code>SLUGs</code> separated by commas):','templatic');?>
	  <input class="widefat" id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>" type="text" value="<?php echo attribute_escape($category); ?>" />
	  </label>
	</p>
	<?php
		}
	}
register_widget('templ_latest_posts_grid_custom_view');

class custom_multi_city extends WP_Widget {
	function custom_multi_city() {
	//Constructor
		$widget_ops = array('classname' => 'Custom Multi City Options', 'description' => __('Custom Multi City Options. It should be once on the page.') );		
		$this->WP_Widget('widget_custom_multi_city', __('T &rarr; Custom Multi City Options'), $widget_ops);
	}
	function widget($args, $instance) {
	// prints the widget
		extract($args, EXTR_SKIP);
		$title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
		$desc1 = empty($instance['desc1']) ? '' : apply_filters('widget_desc1', $instance['desc1']);
		 ?>						
        <div class="widget custom_multi_city">
            <p>Chọn thành phố:&nbsp;</p>
        	<?php
			echo get_multicity_dl('multi_city','multi_city',$_SESSION['multi_city'],'onchange="set_selected_city(this.value)"');?>
        </div>
        
 	<?php
	}
	function update($new_instance, $old_instance) {
	//save the widget
		$instance = $old_instance;		
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['desc1'] = ($new_instance['desc1']);
		return $instance;
	}
	function form($instance) {
	//widgetform in backend
		$instance = wp_parse_args( (array) $instance, array( 'title' => '', 't1' => '', 't2' => '', 't3' => '',  'img1' => '', 'desc1' => '' ) );		
		$title = strip_tags($instance['title']);
		$desc1 = ($instance['desc1']);
?>

<?php
	}}
register_widget('custom_multi_city');

// =============================== Top place Link ======================================

class custom_listing_link extends WP_Widget {
	function custom_listing_link() {
	//Constructor
		$widget_ops = array('classname' => 'widget Featured Video', 'description' => __('Custom Put Add place links and login module. Recommended in the &lsquo;Header: Right area&rsquo;') );
		$this->WP_Widget('widge_custom_listing_link', __('T &rarr; Custom Login & Add place widget'), $widget_ops);
	}

	function widget($args, $instance) {
	// prints the widget

		extract($args, EXTR_SKIP);
		echo $before_widget;
		$title = empty($instance['title']) ? '&nbsp;' : apply_filters('widget_title', $instance['title']);
		?>
        
        
        <ul class="member_link">
    <?php
    global $current_user;
	$user_link = get_author_posts_url($current_user->ID);
if(strstr($user_link,'?') ){$user_link = $user_link.'&list=favourite';}else{$user_link = $user_link.'?list=favourite';}
    if($current_user->ID)
    {
    ?>
    <?php
    if(add_filter('templ_tophdr_welcome_filter',true))
	{
	?>
    <li class="first_li"><?php echo __('Welcome, ','templatic'); ?><a href="<?php echo str_replace(' ','-',$user_link); ?>"><?php echo $current_user->display_name; ?></a></li>
    <?php
	}
	$redirect = site_url().'/?ptype=login';
	?>
    <li><a href="<?php echo site_url();?>/?ptype=profile"><?php _e('Edit profile','templatic');?></a></li>
    <li><a href="<?php echo wp_logout_url(home_url()); ?>"><?php _e('Log out','templatic');?></a></li>
   
    <?php	
    }else
    {
    ?>
	
	<?php if ( get_option('users_can_register') ) { ?>
    <li><a href="<?php echo site_url();?>/?ptype=register"><?php _e('Đăng ký','templatic');?></a></li> <?php } ?>
    <li><a href="<?php echo site_url();?>/?ptype=login"><?php _e('Đăng nhập','templatic');?></a></li> 
    <?php	
    }
	if(get_option('ptthemes_add_place_nav') == 'Yes'){
    ?>
     <li><a href="<?php echo site_url();?>/?ptype=post_listing"><?php _e('Tạo địa điểm','templatic');?></a></li> 
	 <?php } 
	 /*if(get_option('ptthemes_add_event_nav') == 'Yes') { ?>
     <li><a href="<?php echo site_url();?>/?ptype=post_event"><?php _e('Add event','templatic');?></a></li> 
	 <?php } */?>
    </ul>
				
				<?php
		echo $after_widget;
	}

	function update($new_instance, $old_instance) {
	//save the widget
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);

		return $instance;

	}

	function form($instance) {
	//widgetform in backend
		$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'category' => '', 'post_number' => '','video_link' => '' ) );
		$title = strip_tags($instance['title']);

?>
 <p>
          <label for="<?php echo $this->get_field_id('title'); ?>"><?php  _e('Title','templatic')?>:
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" />
          </label>
        </p>
<?php
	}

}
register_widget('custom_listing_link');